package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameRankMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameActivity;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRank;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_LOGIN_SYNC, desc = "登录拉取送财")
public class ChannelEnterNameLoginSyncCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        EnterNameProto.ChannelEnterNameSyncMsg reqMsg = EnterNameProto.ChannelEnterNameSyncMsg.parseFrom(packet.getBytes());

        EnterNameProto.EnterNameDisperseGoldSyncMsg.Builder clientMsg = EnterNameProto.EnterNameDisperseGoldSyncMsg.newBuilder();
        for (int activityId : reqMsg.getActivityIdList()) {
            EnterNameModel model = ChannelEnterNameMgr.getEnterNameModel(activityId);
            if(model != null){
                EnterNameActivity activity = ChannelEnterNameMgr.getEnterNameActivity(activityId);
                if(activity == null){
                    return;
                }
                long userId = reqMsg.getUserKey().getUserId();
                UserEnterName userEnterName = model.getUserEnterName(userId);
                if(userEnterName != null && !userEnterName.isGetDisperseGold()){
                    ActivityGroupMatch groupMatch = model.getActivityGroupMatch(userEnterName.getGroupId());
                    if(groupMatch != null){
                        EnterNameGroupRankModel rankModel = ChannelEnterNameRankMgr.getEnterNameGroupRankModel(activityId, activity.getGroupConditionType(), groupMatch.getRankMatchGroupId());
                        if(rankModel != null){
                            EnterNameProto.EnterNameDisperseGoldMsg.Builder goldBuilder = EnterNameProto.EnterNameDisperseGoldMsg.newBuilder();
                            goldBuilder.setActivityId(activityId);
                            for (EnterNameGroupRank groupRank : rankModel.getRankList()) {
                                EnterNameProto.EnterNameDisperseGoldGroupInfoMsg.Builder builder = EnterNameProto.EnterNameDisperseGoldGroupInfoMsg.newBuilder();
                                builder.setScore(groupRank.getValue().longValue());
                                ActivityGroupMatch temp = model.getMatchMap().get(groupRank.getGroupId());
                                if(temp != null) {
                                    builder.addAllZoneList(temp.getZoneIdList());
                                }
                                goldBuilder.addPk(builder);
                            }
                            clientMsg.addDisGold(goldBuilder);
                        }
                    }
                }
            }
        }

        if(clientMsg.getDisGoldCount() > 0){
            ChannelMessageHelper.sendGamePacket(Protocol.U_ENTER_NAME_DISPERSE_GOLD_SYNC, clientMsg, reqMsg.getUserKey().getChannelId(), reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());
        }
    }

    @Override
    public void sendData() {

    }
}
